package com.norns.torque.entrty

import android.content.Context
import androidx.room.Database
import androidx.room.Room.databaseBuilder
import androidx.room.RoomDatabase
import androidx.room.TypeConverters

@Database(entities = [ScaleBean::class,WifiDevice::class], version = 1)
@TypeConverters(DateTypeConverter::class)
abstract class AppDatabase : RoomDatabase() {
    abstract fun scaleDao():ScaleDao?
    abstract fun deviceDao():WifiDeviceDao?

    companion object {
        private var sInstance: AppDatabase? = null

        @JvmStatic
        fun getInstance(context: Context): AppDatabase {
            sInstance ?: synchronized(this) {
                sInstance ?: createInstance(context).also { sInstance = it }
            }
            return sInstance!!
        }

        private fun createInstance(context: Context): AppDatabase {
            return databaseBuilder(
                context,
                AppDatabase::class.java, "scale.db"
            )
                //.allowMainThreadQueries() //允许主线程访问
                //.addCallback(sRoomDatabaseCallBack)
                .build()

        }
    }
}